package com.fenghuajueli.module_find_the_difference.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

import com.fenghuajueli.libbasecoreui.constants.AppConfigInfo;
import com.fenghuajueli.module_find_the_difference.entity.Zhaocha;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ZhaoView extends AppCompatImageView {
    private static final int CIRCLE_RADIUS = 70;
    private List<Rect> noDianRects = new ArrayList<>();
    private Set<Rect> dianRects = new HashSet<>();
    List<Zhaocha.ListBean> list = new ArrayList<>();
    private Paint paint;
    private Callback callback;

    public ZhaoView(@NonNull Context context) {
        super(context);
        init();
    }

    public ZhaoView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ZhaoView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(8);
        paint.setColor(getResources().getColor(android.R.color.holo_red_light));
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public void setRightPoint(List<Point> list) {
        noDianRects.clear();
        dianRects.clear();
        for (int i = 0; i < list.size(); i++) {
            Point point = list.get(i);
            noDianRects.add(new Rect(point.x - CIRCLE_RADIUS, point.y - CIRCLE_RADIUS, point.x + CIRCLE_RADIUS, point.y + CIRCLE_RADIUS));
        }
        invalidate();
    }

    public void setZhaocha(List<Zhaocha.ListBean> zhaocha) {
        noDianRects.clear();
        dianRects.clear();
        for (int i = 0; i < zhaocha.size(); i++) {
            int x = zhaocha.get(i).x;
            int y = zhaocha.get(i).y;
            int w = zhaocha.get(i).w;
            int h = zhaocha.get(i).h;
            noDianRects.add(new Rect(x, getHeight() - y - h, x + w, getHeight() - y));
        }
        invalidate();
    }

    public void showTip() {
        if (noDianRects.size() > 0) {
            Rect rect = noDianRects.get(0);
            noDianRects.remove(rect);
            dianRects.add(rect);
            invalidate();
            if (callback != null) {
                callback.onTrue(rect.centerX(), rect.centerY());
                if (noDianRects.size() == 0 && callback != null) {
                    callback.onEnd();
                }
            }
        }
    }

    public void addPoint(int x, int y) {
        for (Rect rect : dianRects) {
            if (rect.contains(x, y)) {
                return;
            }
        }
        for (Rect rect : noDianRects) {
            if (rect.contains(x, y)) {
                noDianRects.remove(rect);
                dianRects.add(rect);
                invalidate();
                break;
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (Rect rect : dianRects) {
            canvas.drawCircle(rect.centerX(), rect.centerY(), rect.width() / 2, paint);
        }
        if (AppConfigInfo.APP_DEBUG) {
            for (Rect rect : noDianRects) {
                canvas.drawPoint(rect.centerX(), rect.centerY(), paint);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                int x = (int) event.getX();
                int y = (int) event.getY();
                for (Rect rect : dianRects) {
                    if (rect.contains(x, y)) {
                        return false;
                    }
                }
                for (Rect rect : noDianRects) {
                    if (rect.contains(x, y)) {
                        noDianRects.remove(rect);
                        dianRects.add(rect);
                        invalidate();
                        if (callback != null) {
                            callback.onTrue(x, y);
                            if (noDianRects.size() == 0 && callback != null) {
                                callback.onEnd();
                            }
                        }
                        return true;
                    }
                }
                if (callback != null) {
                    callback.onFalse();
                }
                break;
        }
        return false;
    }

    public interface Callback {

        void onFalse();

        void onTrue(int x, int y);

        void onEnd();
    }
}
